<template>
  <div class="vehicle-container">
    <div class="header">
      <!-- <el-page-header content="详情页面" @back="goBack">
        h
      </el-page-header> -->
    </div>
    <div class="footer">
      <el-card>
        <div class="card-header">
          <el-tabs v-model="activeName" @tab-click="tabClick">
            <el-tab-pane label="基本信息" name="information" />
            <el-tab-pane label="行使证信息" name="drivingLicense" />
          </el-tabs>
        </div>
        <div class="card-footer">
          <div>
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              label-width="auto"
            >
              <el-row type="flex" justify="start">
                <el-form-item
                  :class="{ formItem: disabled }"
                  prop="truckTypeId"
                  label="车辆编号："
                >
                  <el-col :span="8">
                    <el-input
                      v-model="form.truckTypeId"
                      clearable
                      :class="{ inputDisabled: disabled }"
                      disabled
                      class="inputStyle"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item
                  :class="{ formItem: disabled }"
                  prop="licensePlate"
                  label="车牌号码："
                >
                  <el-col :span="8">
                    <el-input
                      v-model="form.licensePlate"
                      clearable
                      :class="{ inputDisabled: disabled }"
                      :disabled="disabled"
                      class="inputStyle"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item
                  :class="{ formItem: disabled }"
                  prop="truckTypeName"
                  label="车型名称："
                >
                  <el-col :span="8">
                    <el-select
                      v-model="form.truckTypeName"
                      clearable
                      :class="{ inputDisabled: disabled }"
                      :disabled="disabled"
                      placeholder="请选择"
                      class="inputStyle"
                    >
                      <el-option
                        v-for="item in vehiceTypeList"
                        :key="item.id"
                        :value="item.name"
                        :label="item.name"
                      />
                    </el-select>
                  </el-col>
                </el-form-item>
              </el-row>
              <el-row type="flex" justify="start">
                <el-form-item
                  :class="{ formItem: disabled }"
                  prop="allowableVolume"
                  label="车辆体积："
                >
                  <el-col :span="8">
                    <el-input
                      v-model="form.allowableVolume"
                      clearable
                      :class="{ inputDisabled: disabled }"
                      :disabled="disabled"
                      class="inputStyle"
                    />
                  </el-col>
                </el-form-item>

                <el-form-item
                  :class="{ formItem: disabled }"
                  prop="allowableLoad"
                  label="车辆载重："
                >
                  <el-col :span="8">
                    <el-input
                      v-model="form.allowableLoad"
                      clearable
                      :class="{ inputDisabled: disabled }"
                      :disabled="disabled"
                      class="inputStyle"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item
                  :class="{ formItem: disabled }"
                  prop="deviceGpsId"
                  label="GPSID："
                >
                  <el-col :span="8">
                    <el-input
                      v-model="form.deviceGpsId"
                      clearable
                      :class="{ inputDisabled: disabled }"
                      :disabled="disabled"
                      class="inputStyle"
                    />
                  </el-col>
                </el-form-item>
              </el-row>
              <el-row type="flex" justify="start">
                <el-form-item
                  :class="{ formItem: disabled }"
                  prop=""
                  label="图片信息："
                  style="width: 100%"
                >
                  <el-col v-if="disabled">
                    <ul class="displayImg">
                      <li v-for="(item, index) in imgList" :key="item.url">
                        <img :src="item.url" alt="">
                        <i
                          class="el-icon-zoom-in icon"
                          @click="magnifyClick(index)"
                        />
                      </li>
                    </ul>
                  </el-col>
                  <el-col v-else>
                    <el-upload
                      action=""
                      :http-request="uploadImg"
                      list-type="picture-card"
                      :on-preview="handleImgPreview"
                      :on-remove="handleRemove"
                      :file-list="imgList"
                      class="upload-demo"
                    >
                      <div class="upload-box">
                        <i class="upload-icon" />
                        <span class="upload-text">上传图片</span>
                      </div>
                      <div slot="file" slot-scope="{ file }">
                        <img
                          class="el-upload-list__item-thumbnail uploadImg"
                          :src="file.url"
                          alt=""
                        >
                        <div class="iconBox">
                          <span class="el-upload-list__item-actions">
                            <span
                              class="el-upload-list__item-preview"
                              @click="viewImg(file)"
                            >
                              <i class="el-icon-zoom-in" />
                            </span>
                            <span
                              v-if="!disabled"
                              class="el-upload-list__item-delete"
                              @click="handleRemove(file)"
                            >
                              <i class="el-icon-delete" />
                            </span>
                          </span>
                        </div>
                      </div>
                    </el-upload>
                    <span
                      v-show="!disabled"
                      class="prompt-text"
                    >注：图片大小不超过5M；仅能上传 PNG JPEG
                      JPG类型图片；建议上传400*300尺寸的图片；最多上传2张</span>
                  </el-col>
                </el-form-item>
              </el-row>
              <el-row type="flex" justify="center">
                <el-form-item class="last-FormItem">
                  <template>
                    <el-button
                      v-show="disabled"
                      type="success"
                      size="small"
                      class="buttonStyle"
                      @click="editClick"
                    >编辑</el-button>
                    <el-button
                      v-show="!disabled"
                      size="small"
                      class="buttonStyle"
                      type="success"
                      @click="saveClick"
                    >保存</el-button>
                    <el-button
                      v-show="!disabled"
                      size="small"
                      class="buttonStyle"
                      type="success"
                      plain
                      @click="cancelClick"
                    >取消</el-button>
                  </template>
                </el-form-item>
              </el-row>
            </el-form>
          </div>
        </div>
      </el-card>
    </div>
    <!-- 图片展示 -->
    <el-dialog :visible.sync="imgdialogVisible" width="35%">
      <div class="dialogBox">
        <img width="100%" :src="imgUrl" alt="" style="vertical-align: bottom">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getVehicleDetailAPI,
  getVehicleTypeListAPI,
  updateVehicleAPI
} from '@/api/vehicle'
export default {
  data() {
    return {
      // 车辆id
      id: this.$route.params.id,
      // input是否可编辑
      disabled: true,
      // 车型数据
      vehiceTypeList: [],
      // 表单数据
      form: {
        allowableLoad: 0,
        allowableVolume: 0,
        deviceGpsId: '',
        licensePlate: '',
        picture: '',
        truckTypeId: 0,
        truckTypeName: ''
      },
      // 校验规则
      rules: {
        truckTypeId: [
          { requried: true, message: '请输入编号', trigger: 'blur' }
        ],
        allowableLoad: [
          { requried: true, message: '请输入载重', trigger: 'blur' }
        ],
        allowableVolume: [
          { requried: true, message: '请输入体积', trigger: 'blur' }
        ],
        deviceGpsId: [
          { requried: true, message: '请输入GPS', trigger: 'blur' }
        ],
        licensePlate: [
          { requried: true, message: '请输入车牌号码', trigger: 'blur' }
        ],
        truckTypeName: [
          { requried: true, message: '请选择', trigger: 'blur' }
        ]
      },
      // tab栏
      activeName: 'information',
      // 图片弹层
      imgdialogVisible: false,
      // 图片地址
      imgUrl: '',
      // 图片列表
      imgList: []
    }
  },
  created() {
    this.tableDataList()
    // 车型数据
    getVehicleTypeListAPI().then(({ data }) => {
      this.vehiceTypeList = data.data
    })
  },
  methods: {
    // 表格数据
    async tableDataList() {
      const { data } = await getVehicleDetailAPI(this.id)
      const result = data.data.picture.split(',')
      this.imgList = result.map((item) => ({
        url: item
      }))
      this.form = data.data
    },
    // 编辑
    async editClick() {
      this.tableDataList()
      this.disabled = false
    },
    // 保存
    async saveClick() {
      await this.$refs.form.validate()
      await updateVehicleAPI(this.id, this.form)
      this.disabled = true
    },
    // 取消
    cancelClick() {
      this.tableDataList()
      this.disabled = true
    },
    // 图片弹层
    viewImg(file) {
      this.imgUrl = file.url
      this.imgdialogVisible = true
    },
    // 放大图片
    magnifyClick(index) {
      this.imgUrl = this.imgList[index].url
      this.imgdialogVisible = true
    },

    handleImgPreview(file) {
      console.log(file)
    },
    // 删除图片
    handleRemove(file) {
      this.imgList.splice(this.imgList.indexOf(file), 1)
      this.form.picture = this.imgList.map((item) => item.url).join(',')
    },
    // 上传图片
    uploadImg() {},
    // tab栏点击
    tabClick() {
      this.disabled = true
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  padding: 10px 20px;
  .card-footer {
    padding: 10px 40px;
    .el-form-item {
      padding: 0 20px;
      width: 460px;

      ::v-deep .el-form-item__label {
        font-size: 13px;
      }
      .inputStyle {
        width: 220px;
      }
      .inputDisabled {
        ::v-deep .el-input__inner {
          border: none !important;
          color: #000 !important;
          font-size: 13px !important;
          background-color: transparent !important;
        }
      }

      .upload-demo {
        .uploadImg {
          width: 210px;
          height: 146px;
          border-radius: 6px;
        }
        .upload-icon {
          margin: 0 auto;
          transform: translateY(80%);
          position: relative;
          display: flex;
          justify-content: center;
          align-items: center;
          width: 50px;
          height: 50px;
          &::before {
            content: "";
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 25px;
            height: 25px;
            background: url("../../../assets/icon_upload@2x.png") no-repeat;
            background-size: 25px;
          }
        }
        .upload-text {
          display: inline-block;
          transform: translateY(-150%);
          line-height: 1.5;
          color: #8c939d;
          font-size: 13px;
        }
      }
    }
    .prompt-text {
      display: inline-block;
      margin: 5px 0;
      line-height: 1.5;
      color: #8c939d;
      font-size: 12px;
    }
    .formItem {
      margin: 0;

      .displayImg {
        list-style: none;
        width: 100%;
        height: 100%;
        li {
          position: relative;
          display: inline-block;
          margin-left: 10px;
          width: 210px;
          height: 146px;
          border-radius: 6px;
          border: 1px solid #c0ccda;

          img {
            width: 100%;
            height: 100%;
            border-radius: 6px;
          }

          .icon {
            position: absolute;
            top: 50%;
            left: 50%;
            z-index: 20;
            transform: translate(-50%, -50%);
            font-size: 20px;
            color: #fff;
            visibility: hidden;
            transition: opacity 0.3s;
          }
          &::after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            width: 210px;
            height: 146px;
            z-index: 11;
            transition: background-color 0.3s;
          }

          &:hover {
            &::after {
              transition: background-color 0.3s;
              border-radius: 6px;
              background-color: rgba(0, 0, 0, 0.4);
            }
            & .icon {
              visibility: visible;
              transition: opacity 0.3s;
              opacity: 0.8;
            }
          }
        }
      }
    }
    .last-FormItem {
      margin: 0;
      ::v-deep .el-form-item__content {
        margin: 0;
        text-align: center;
      }
      .buttonStyle {
        width: 80px;
        height: 30px;
        transform: translateX(-50%);

        &.buttonStyle:first-child {
          background-color: #189450;
          border-color: #189450;
          margin-right: 10px;

          &:hover {
            opacity: 0.7;
          }
        }
      }
    }
    .dialogBox {
      float: left;
      width: 100%;
      height: 100%;
      text-align: center;
    }
  }
}
// tab栏样式
::v-deep .el-tabs--top .el-tabs__item.is-top:nth-child(2) {
  padding-left: 20px;
}
::v-deep .el-tabs__active-bar {
  height: 3px;
  background-color: #189450;
}
::v-deep .el-tabs__item.is-active {
  color: #189450;
}
::v-deep .el-tabs__item {
  font-size: 14px;
  font-weight: bold;
}
// 文件上传样式
::v-deep .el-upload-list__item-actions {
  width: 210px;
}
::v-deep .el-upload--picture-card {
  width: 210.22px;
  &:hover .upload-icon::before,
  &:hover span {
    color: #189450;
  }
  &:hover {
    border-color: #189450;
  }
}
::v-deep .el-upload-list--picture-card .el-upload-list__item {
  width: 210.22px;
}
// 弹窗样式
::v-deep .el-dialog__header {
  padding: 0;
}
::v-deep .el-dialog__body {
  padding: 0;
}
</style>
